package com.twetec.cms.adapter.html.frondend.config;

import com.google.common.base.Predicates;
import com.twetec.cms.adapter.html.frondend.response.ResultCodeMsgEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static List<ResponseMessage> globalResponseMessageList = Arrays.asList(
            new ResponseMessageBuilder().code(ResultCodeMsgEnum.SUCCESS.getCode()).message(ResultCodeMsgEnum.SUCCESS.getMsg()).build(),
            new ResponseMessageBuilder().code(ResultCodeMsgEnum.FAILURE.getCode()).message(ResultCodeMsgEnum.FAILURE.getMsg()).build()
    );

//    @Bean
//    public Docket learnerAPIConfig(Environment environment) {
//        // 运行环境判断
//        Profiles profiles = Profiles.of("dev", "test");
//        boolean needSwaggered = environment.acceptsProfiles(profiles);
//
//        return new Docket(DocumentationType.SWAGGER_2)
//                .groupName("learner")
//                .apiInfo(learnerAPIInfo())
//                .useDefaultResponseMessages(false)
//                .globalResponseMessage(RequestMethod.GET, globalResponseMessageList)
//                .globalResponseMessage(RequestMethod.POST, globalResponseMessageList)
//                .globalResponseMessage(RequestMethod.PUT, globalResponseMessageList)
//                .globalResponseMessage(RequestMethod.DELETE, globalResponseMessageList)
//                .enable(needSwaggered)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.twetec.eol.adapter.mservice.pc.learner"))
//                .paths(Predicates.and(PathSelectors.regex("/learner/.*")))
//                .build();
//    }

//    private ApiInfo learnerAPIInfo() {
//        return new ApiInfoBuilder()
//                .title("PC适配层-学员端接口文档")
//                .description("包含了PC适配层，学员端的接口")
//                .version("v1.0")
//                .build();
//    }

    @Bean
    public Docket docketConfig(Environment environment) {
        // 运行环境判断
        Profiles profiles = Profiles.of("dev", "test");
        boolean needSwaggered = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("manager")
                .apiInfo(docketAPIInfo())
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, globalResponseMessageList)
                .globalResponseMessage(RequestMethod.POST, globalResponseMessageList)
                .globalResponseMessage(RequestMethod.PUT, globalResponseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, globalResponseMessageList)
                .enable(needSwaggered)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.twetec.cms.adapter.html.frondend"))
                .paths(Predicates.and(PathSelectors.regex("/.*")))
                .build();
    }

    private ApiInfo docketAPIInfo() {
        return new ApiInfoBuilder()
                .title("资讯发布系统-静态页面接口文档")
                .description("包含了资讯发布系统静态页面的接口")
                .version("v1.0")
                .build();
    }

//    @Bean
//    public Docket teacherAPIConfig(Environment environment) {
//        // 运行环境判断
//        Profiles profiles = Profiles.of("dev", "test");
//        boolean needSwaggered = environment.acceptsProfiles(profiles);
//
//        return new Docket(DocumentationType.SWAGGER_2)
//                .groupName("impl")
//                .apiInfo(teacherAPIInfo())
//                .useDefaultResponseMessages(false)
//                .globalResponseMessage(RequestMethod.GET, globalResponseMessageList)
//                .globalResponseMessage(RequestMethod.POST, globalResponseMessageList)
//                .globalResponseMessage(RequestMethod.PUT, globalResponseMessageList)
//                .globalResponseMessage(RequestMethod.DELETE, globalResponseMessageList)
//                .enable(needSwaggered)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.twetec.eol.adapter.mservice.pc.impl"))
//                .paths(Predicates.and(PathSelectors.regex("/impl/.*")))
//                .build();
//    }

//    private ApiInfo teacherAPIInfo() {
//        return new ApiInfoBuilder()
//                .title("PC适配层-讲师端接口文档")
//                .description("包含了PC适配层，讲师端的接口")
//                .version("v1.0")
//                .build();
//    }

}
